package com.karson.kafka.ch01;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author Karson Wang
 * @title ProducerFastStart
 * @Description 描述
 * @Date 2020/4/23 19:18
 * @Copyright 2019-2020
 */
public class ProducerFastStart {
    public static final String brokerList = "192.168.2.178:9092";
    public static final String topic = "topic-demo";

    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.put("bootstrap.servers", brokerList);
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        // 配置生产者客户端参数并创建KafkaProducer实例
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(properties);

        // 构建发送消息
        ProducerRecord<String, String> record = new ProducerRecord<>(topic, "Hello, Kafka!");

        // 发送消息
        producer.send(record);

        // 关闭生产者客户端实例
        producer.close();
    }
}
